<!-- （Modal） -->
<div class="modal fade" id="assignedModal" tabindex="-1" role="dialog"
     aria-labelledby="assignedModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"
                aria-hidden="true">
          ×
        </button>
        <h4 class="modal-title" id="assignedModalLabel">
          <span class="glyphicon glyphicon-user"></span> 分派
        </h4>
      </div>
      <div class="modal-body">
        <div class="row container-fluid">
          <form class="form-horizontal" data-jsonajax="true" data-istip="false" action="/xms/api/data/assign" method="post">
            <input type="hidden" name="entityid" />
            <input type="hidden" name="ownerid" />
            <div class="form-group col-sm-12">
              <span class="glyphicon glyphicon-warning-sign"></span>
              <span class="text-muted">已选择 1 个记录，要将他们分派给谁？</span>
            </div>
            <div class="form-group col-sm-12">
              <label><input type="radio" name="owneridtype" value="1" checked /> 分派给我</label>
            </div>
            <div class="form-group col-sm-12">
              <label><input type="radio" name="owneridtype" value="2" /> 分派给其他人</label>
              <div class="owner_selector hide">
                <input type="text" id="ownerid" name="ownerid_text" class="form-control input-sm lookup" />
              </div>
            </div>
            <div class="form-group col-sm-12">
              <label><input type="radio" name="owneridtype" value="3" /> 分派给团队</label>
              <div class="team_selector hide">
                <input type="text" id="teamid" name="teamid_text" class="form-control input-sm lookup" />
              </div>
            </div>
          </form>
        </div>
        <script src="/content/js/jquery.form.js"></script>
        <script>
          var paramsModel = JSON.parse(decodeURIComponent($('#assignedModal').parent().attr('data-send')));
          var AssignedModel = {
            dialog: $('#assignedModal'),
            dialogid: 'assignedModal',
            entityid: paramsModel.entityid,
            objectid: paramsModel.objectid,
            ownerid: '',
            singlemode: true,
            initForm: function () {
              var $form = AssignedModel.dialog.find('form');
              $form.find('input[name=entityid]').val(AssignedModel.entityid);
              $(AssignedModel.objectid).each(function(i, n){
                $form.append('<input type="hidden" data-hiddendata="true" name="objectid['+i+']" value="'+n+'" />');
              });
              Xms.Web.Form($form, function (response) {
                console.log(response);
                if (response.IsSuccess == false) {
                  Xms.Web.Toast(response.Content || response.ErrorMessage, false);
                  AssignedModel.resetForm();
                } else {
                  AssignedModel.dialog.modal('hide');
                }
              });
            },
            submit: function () {
              AssignedModel.disabledForm();
              var $form = AssignedModel.dialog.find('form');
              $form.submit();
            },
            dialog_return: function () {
              AssignedModel.dialog.modal('hide');
            },
            disabledForm: function () {
              AssignedModel.dialog.find('button').prop('disabled', true);
            },
            resetForm: function () {
              AssignedModel.dialog.find('button').prop('disabled', false);
            },
            bindselected: function (result, inputid) {
              console.log(result);
              if (!result || result.length == 0) return;
              AssignedModel.dialog.find('#' + inputid).val(result[0].name);
              AssignedModel.dialog.find('input[name=ownerid]').val(result[0].id);
              var target = [];
              $(result).each(function (i, n) {
                target.push(n.id);
              });
              console.log(target);
            }
          };
          $(function () {
            AssignedModel.initForm();
            AssignedModel.dialog.modal({
              //backdrop: 'static'
            });
            AssignedModel.dialog.on('hidden.bs.modal', function () {
              Xms.Web.CloseDialog(AssignedModel.dialogid);
            });
            AssignedModel.dialog.find('input[name=owneridtype]').on('click', null, function (e) {
              var type = $(e.target).val();
              if (type == 1) {
                $('.owner_selector').addClass('hide');
                $('.team_selector').addClass('hide');
              }
              else if (type == 2) {
                $('.owner_selector').removeClass('hide');
                $('.team_selector').addClass('hide');
              }
              else if (type == 3) {
                $('.owner_selector').addClass('hide');
                $('.team_selector').removeClass('hide');
              }
            });
            AssignedModel.dialog.find('#ownerid').lookup({
              dialog: function () { Xms.Web.OpenDialog('/entity/RecordsDialog?entityname=systemuser&singlemode=true&inputid=ownerid', 'AssignedModel.bindselected'); }
              , clear: function () {
                $('#' + inputid).val('');
                $('#' + valueid).val('');
                return false;
              }
            });
            AssignedModel.dialog.find('#teamid').lookup({
              dialog: function () { Xms.Web.OpenDialog('/entity/RecordsDialog?entityname=team&singlemode=true&inputid=teamid', 'AssignedModel.bindselected'); }
              , clear: function () {
                $('#' + inputid).val('');
                $('#' + valueid).val('');
                return false;
              }
            });
            AssignedModel.dialog.find('form').ajaxForm(function (response) {
              if (response.IsSuccess) {
                Xms.Web.Alert(true, response.Content);
                AssignedModel.dialog_return();
                AssignedModel.resetForm();
                Xms.Web.Event.publish('refresh');
                location.reload(true);
                return;
              }
              Xms.Web.Alert(false, response.Content || response.ErrorMessage);
              AssignedModel.resetForm();
            });
          });
        </script>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default"
                data-dismiss="modal">
          <span class="glyphicon glyphicon-remove"></span>
          关闭
        </button>
        <button type="button" class="btn btn-primary" onclick="AssignedModel.submit()">
          <span class="glyphicon glyphicon-ok"></span>
          确定
        </button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->